package jxta.chat.entities;


import jxta.chat.util.Constants;
import jxta.chat.util.Constants.MessageParamsKeys;
import net.jxta.endpoint.Message;
import net.jxta.endpoint.MessageElement;
import net.jxta.endpoint.StringMessageElement;
import net.jxta.protocol.PipeAdvertisement;

/**
 * A class that creates Login messages
 *
 */
public class LoginMessage extends AbstractMessage{

	public final static String BIDI_PIPE_ATTRIBUTE = "BidiPipeAdv";
	
	public LoginMessage(){
		
	}
	
	public LoginMessage(Message msg){
		super(msg);
	}
	
	/**
	 * Create a new Message containing the given pipe advertisement
	 * 
	 * @return a message that contains the pipe advertisement sent in the constructor
	 */
	@Override
	public Message createMessage(MessageParams params){
		
		Message retVal = super.createMessage(params);
		
		MessageElement me = new StringMessageElement(BIDI_PIPE_ATTRIBUTE, params.getParameter(MessageParamsKeys.PIPE_ADV).toString(), null);
		
		retVal.addMessageElement(me);
		
		return msg = retVal;
	}
	
	public String getMessageType(){
		return Constants.MessageTypes.LOGIN.getName();
	}
	
	public String getPipeAdvertisment(){
		MessageElement msgElement = msg.getMessageElement(null, LoginMessage.BIDI_PIPE_ATTRIBUTE);
		
		return msgElement != null ? msgElement.toString() : null;
	}
	
	public MessageElement getPipeAdvertismentElement(){
		return msg.getMessageElement(null, LoginMessage.BIDI_PIPE_ATTRIBUTE);
	}
}
